package com.example.timmy.myapplication.presenter;

import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;

import com.example.timmy.myapplication.R;
import com.example.timmy.myapplication.ui.activity.AndroidChatActivity;
import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.LimitLine;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.interfaces.datasets.ILineDataSet;
import com.github.mikephil.charting.utils.Utils;

import java.util.ArrayList;

/**
 * @Auther: Timmy
 * @Data: 2017/9/21
 * @Fuction:
 */

public class AndroidChatActivityPresenter {

    private AndroidChatActivity mChatActivity;
    private LineChart mLineChart;

    public AndroidChatActivityPresenter(AndroidChatActivity chatActivity) {
        mChatActivity = chatActivity;
    }

    /**
     * 初始化 Chart 的数据
     */
    public void initData(LineChart mChart) {
        mLineChart = mChart;

        //x轴
        LimitLine llXAxis = new LimitLine(10f, "标记");
        //设置线宽
        llXAxis.setLineWidth(4f);
        //
        llXAxis.enableDashedLine(10f, 10f, 0f);
        //设置
        llXAxis.setLabelPosition(LimitLine.LimitLabelPosition.LEFT_BOTTOM);
        llXAxis.setTextSize(10f);

        XAxis xAxis = mChart.getXAxis();
        xAxis.enableGridDashedLine(10f, 10f, 0f);
        //设置x轴的最大值
        xAxis.setAxisMaximum(50f);
        //设置x轴的最小值
        xAxis.setAxisMinimum(0f);
        // 设置X轴的位置
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM); // 设置X轴的位置

        LimitLine ll1 = new LimitLine(150f, "优秀");
        ll1.setLineWidth(4f);
        ll1.enableDashedLine(10f, 10f, 0f);
        ll1.setLabelPosition(LimitLine.LimitLabelPosition.RIGHT_TOP);
        ll1.setTextSize(10f);

        LimitLine ll2 = new LimitLine(30f, "不及格");
        ll2.setLineWidth(4f);
        ll2.enableDashedLine(10f, 10f, 0f);
        ll2.setLabelPosition(LimitLine.LimitLabelPosition.RIGHT_BOTTOM);
        ll2.setTextSize(10f);

        // --------------- 左边 Y 轴 --------------------------
        YAxis leftAxis = mChart.getAxisLeft();
        //重置所有限制线,以避免重叠线
        // leftAxis.removeAllLimitLines();
        //设置优秀线
        // leftAxis.addLimitLine(ll1);
        //设置及格线
        // leftAxis.addLimitLine(ll2);
        //y轴最大
        leftAxis.setAxisMaximum(200f);
        //y轴最小
        leftAxis.setAxisMinimum(0f);
        // 第二个参数是否是虚线
        leftAxis.enableGridDashedLine(10f, 0f, 0f);
        leftAxis.setDrawZeroLine(false);

        // 限制数据(而不是背后的线条勾勒出了上面)
        leftAxis.setDrawLimitLinesBehindData(true);

        // --------------- 右边 Y 轴 --------------------------

        YAxis rightAxis = mChart.getAxisRight();
        rightAxis.setTextColor(Color.RED);
        rightAxis.setAxisMaximum(200f);
        rightAxis.setAxisMinimum(-200);
        rightAxis.setDrawGridLines(false);
        rightAxis.setDrawZeroLine(false);
        rightAxis.setGranularityEnabled(false);

        // ----------------------------------------------------
        mChart.getAxisRight().setEnabled(false);

        //这里我模拟一些数据
        ArrayList<Entry> values = new ArrayList<>();
        values.add(new Entry(0, 50));
        values.add(new Entry(10, 66));
        values.add(new Entry(15, 120));
        values.add(new Entry(20, 30));
        values.add(new Entry(25, 10));
        values.add(new Entry(30, 10));
        values.add(new Entry(35, 10));
        values.add(new Entry(40, 110));
        values.add(new Entry(45, 30));
        values.add(new Entry(50, 100));
        // values.add(new Entry(100, 30));

        //设置数据
        setData(values);

        //默认动画
        // mChart.animateX(2500);
        //刷新
        //mChart.invalidate();
    }

    private void setData(ArrayList<Entry> values) {

        LineDataSet set1;

        if (mLineChart.getData() != null && mLineChart.getData().getDataSetCount() > 0) {
            set1 = (LineDataSet) mLineChart.getData().getDataSetByIndex(0);
            set1.setValues(values);
            mLineChart.getData().notifyDataChanged();
            mLineChart.notifyDataSetChanged();
        } else {
            // 创建一个数据集,并给它一个类型
            set1 = new LineDataSet(values, "");

            // 在这里设置线
            set1.enableDashedLine(1f, 0f, 0f);
            set1.enableDashedHighlightLine(10f, 5f, 0f);
            set1.setColor(Color.BLACK);
            set1.setCircleColor(Color.BLUE);
            set1.setLineWidth(1f);
            set1.setCircleRadius(3f);
            set1.setDrawCircleHole(false);
            set1.setValueTextSize(9f);
            set1.setDrawFilled(true);
            set1.setFormLineWidth(1f);
            set1.setFormLineDashEffect(new DashPathEffect(new float[]{10f, 5f}, 0f));
            set1.setFormSize(15.f);

            // 线与线是否有值
            set1.setDrawValues(false);
            // 线圆滑
            // set1.setMode(LineDataSet.Mode.CUBIC_BEZIER);
            // 直线
            set1.setMode(LineDataSet.Mode.LINEAR);

            if (Utils.getSDKInt() >= 18) {
                // 填充背景只支持18以上
                Drawable drawable = ContextCompat.getDrawable(mChatActivity, R.drawable.fade_red);
                set1.setFillDrawable(drawable);
                // set1.setFillColor(Color.YELLOW);
            } else {
                set1.setFillColor(Color.BLACK);
            }

            ArrayList<ILineDataSet> dataSets = new ArrayList<>();
            //添加数据集
            dataSets.add(set1);

            //创建一个数据集的数据对象
            LineData data = new LineData(dataSets);

            //谁知数据
            mLineChart.setData(data);
        }
    }
}
